package com.my.wemedia.controller.v1;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.my.file.service.FileStorageService;
import com.my.model.common.dtos.PageResponseResult;
import com.my.model.common.dtos.ResponseResult;
import com.my.model.common.enums.AppHttpCodeEnum;
import com.my.model.wemedia.dtos.WmMaterialDto;
import com.my.model.wemedia.pojos.WmMaterial;
import com.my.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {
    @Autowired
    private WmMaterialService wmMaterialService;
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传图片素材
     * @param multipartFile
     * @return
     */
    @PostMapping("/upload_picture")
    public ResponseResult uploadPicture(MultipartFile multipartFile){
        return wmMaterialService.uploadPicture(multipartFile);
    }

    /**
     * 展示图片素材
     * @param wmMaterialDto
     * @return
     */
    @PostMapping("/list")
    public PageResponseResult list(@RequestBody WmMaterialDto wmMaterialDto){
        return wmMaterialService.findList(wmMaterialDto);
    }

    /**
     * 收藏素材
     * @param id
     * @return
     */
    @GetMapping("/collect/{id}")
    public ResponseResult<String> Collect(@PathVariable Integer id){
        //1.参数校验
        if(id == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.修改数据库
        LambdaUpdateWrapper<WmMaterial> luw = new LambdaUpdateWrapper<>();
        luw.eq(WmMaterial::getId,id);
        luw.set(WmMaterial::getIsCollection,1);
        wmMaterialService.update(luw);
        return ResponseResult.okResult("收藏成功");
    }

    /**
     * 取消收藏
     * @param id
     * @return
     */
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult<String> cancelCollect(@PathVariable Integer id){
        //1.参数校验
        if(id == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.修改数据库
        LambdaUpdateWrapper<WmMaterial> luw = new LambdaUpdateWrapper<>();
        luw.eq(WmMaterial::getId,id);
        luw.set(WmMaterial::getIsCollection,0);
        wmMaterialService.update(luw);
        return ResponseResult.okResult("取消收藏成功");
    }

    /**
     * 删除素材
     * @param id
     * @return
     */
    @GetMapping("/del_picture/{id}")
    public ResponseResult delPicture(@PathVariable Integer id) {
        return wmMaterialService.deleteMaterial(id);
    }
}
